package com.ftwj.controller;


import com.ftwj.entity.SysUser;
import com.ftwj.entity.UserBaseInfo;
import com.ftwj.service.SysMenuListService;
import com.ftwj.service.SysUserRoleService;
import com.ftwj.service.SysUserService;
import com.ftwj.vo.UserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@RestController
@RequestMapping("/users")
public class UserListController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysMenuListService sysMenuListService;
    @PostMapping("/search")
    public PageInfo<UserVO> queryUsers(@RequestBody UserVO userVO) {
        Integer pageNum = userVO.getPageNumber();
        Integer pageSize = userVO.getPageSize();
        // 初始化分页，设置当前页码和每页大小
        PageHelper.startPage(pageNum, pageSize);

        // 调用服务层的查询方法，PageHelper会自动处理分页
        List<UserVO> userList = sysUserService.queryUsers(userVO.getUserName(), userVO.getSex(), userVO.getPhoneNumber());

        // 将查询结果封装到PageInfo对象中，以提供分页信息
        PageInfo<UserVO> pageInfo = new PageInfo<>(userList);

        return pageInfo;
    }

    @PostMapping("/add")
    public void addUser(@RequestBody UserVO userVO) {
        SysUser sysUser = userVO.getSysUser();
        UserBaseInfo baseInfo = userVO.getBaseInfo();
        sysUserService.addUser(sysUser, baseInfo);
    }

    @DeleteMapping("/delete/{id}")
    public void deleteUser(@PathVariable String id) {

        sysUserService.deleteUser(id);
    }

    @PostMapping("/update")
    public void updateUser(@RequestBody UserVO userVO) {
        SysUser sysUser = new SysUser();
        sysUser.setId(userVO.getId());
        sysUser.setNickName(userVO.getNickName());
        sysUser.setPhoneNumber(userVO.getPhoneNumber());

        UserBaseInfo baseInfo = new UserBaseInfo();
        baseInfo.setUserName(userVO.getUserName());
        baseInfo.setUserId(userVO.getId());
        baseInfo.setSex(userVO.getSex());
        baseInfo.setRemark(userVO.getRemark());
        baseInfo.setCollegeName(userVO.getCollegeName());
        baseInfo.setClassName(userVO.getClassName());
        baseInfo.setEmail(userVO.getEmail());

        sysUserService.updateUser(sysUser, baseInfo);
    }
    @PostMapping("/assignRole")
    public String assignRoleToUser(@RequestParam("userId") String userId,
                                   @RequestBody List<String> roleIds) {
        sysUserRoleService.assignRolesToUser(userId, roleIds);
        return "Role assignment successful";
    }

    /**
     * 获取当前登录用户的信息。
     *
     * @param id 当前用户的ID，通常从JWT或其他认证机制中获取。
     * @return 当前用户的详细信息。
     */
//    @GetMapping("/me")
//    public UserVO getMyInfo(@RequestParam("id") String id) {
//        // 使用服务层方法获取指定ID的用户信息
//        UserVO userInfo = sysUserService.getUser(id);
//
//        return userInfo;
//    }
    @GetMapping("/me")
    public ResponseEntity<UserVO> getUserDetails(@RequestParam("id") String id) {
        UserVO user = sysUserService.getUser(id);
        if (user == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(user);
    }

}



